#!/bin/bash

SHELL_PATH=`pwd`

#需要编译FFpmeg版本号
FF_VERSION="4.3.1"

SOURCE="ffmpeg-$FF_VERSION"

FF_PATH=$SHELL_PATH/$SOURCE

if [ ! -r $SOURCE ]
then
    if [ ! -f "$SOURCE.tar.bz2" ]
    then
        echo "$SOURCE source not found, Trying to download..."
        curl http://www.ffmpeg.org/releases/$SOURCE.tar.bz2 | tar xj || exit 1
    else
        mkdir $FF_PATH
        tar zxvf $SHELL_PATH/"$SOURCE.tar.bz2" --strip-components 1 -C $FF_PATH || exit 1
    fi
fi

cd $FF_PATH
export TMPDIR=$FF_PATH/tmpdir
mkdir $TMPDIR


API=14
NDK=/Users/baimingjiang/libs/android-ndk-r21b
TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/darwin-x86_64/


FF_CONFIGURE_FLAGS=""

FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --enable-hwaccels --disable-postproc"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-encoders"
# FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-encoders --enable-encoder=h264,aac,mpeg*"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-encoders --enable-encoder=mjpeg"
#FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-decoders --enable-decoder=h264,aac,mpeg*"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --enable-mediacodec --enable-decoder=h264_mediacodec,hevc_mediacodec,mpeg4_mediacodec,vp8_mediacodec,vp9_mediacodec"
# FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-muxers --enable-muxer=h264,aac,pcm_*,flv,mp4,avi,rtsp"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --enable-muxers"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-demuxers --enable-demuxer=mp4,h264,aac,avi,flv,rtsp,hls,*mpeg*,pcm_*"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-parsers --enable-parser=h264,aac,*jpeg*,mpeg*"
#FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-protocol=sctp,ftp,concat,icecast"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-bsfs --enable-bsf=aac_adtstoasc --enable-bsf=h264_mp4toannexb --enable-bsf=null --enable-bsf=noise"
FF_CONFIGURE_FLAGS="$FF_CONFIGURE_FLAGS --disable-filters --enable-avfilter --enable-filter=anull"

function build_android
{
echo "Compiling FFmpeg for $CPU"
./configure \
    --prefix=$PREFIX \
    --enable-static \
    --disable-shared \
    --enable-pic \
    --enable-nonfree \
    --enable-gpl \
    --enable-small \
    --enable-version3 \
    --enable-jni \
    --enable-cross-compile \
    --disable-runtime-cpudetect \
    --disable-stripping \
    --disable-doc \
    --disable-debug \
    --disable-ffmpeg \
    --disable-ffprobe \
    --disable-programs \
    --disable-ffplay \
    --target-os=android \
    --cross-prefix=$CROSS_PREFIX \
    --arch=$ARCH \
    --cpu=$CPU \
    --cc=$CC
    --cxx=$CXX
    --enable-cross-compile \
    --sysroot=$SYSROOT \
    --extra-cflags="-Os -fpic $OPTIMIZE_CFLAGS" \
    --extra-ldflags="$ADDI_LDFLAGS" \
    $FF_CONFIGURE_FLAGS \
    $ADDITIONAL_CONFIGURE_FLAG

make clean
make
make install

rm -rf "$PREFIX/share"
rm -rf "$PREFIX/lib/pkgconfig"

echo "The Compilation of FFmpeg for $CPU is completed"
}

#armv8-a
ARCH=arm64
CPU=armv8-a
CC=$TOOLCHAIN/bin/aarch64-linux-android$API-clang
CXX=$TOOLCHAIN/bin/aarch64-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/aarch64-linux-android-
PREFIX=$SHELL_PATH/android/$CPU
OPTIMIZE_CFLAGS="-march=$CPU"
build_android

#x86
ARCH=x86
CPU=x86
CC=$TOOLCHAIN/bin/i686-linux-android$API-clang
CXX=$TOOLCHAIN/bin/i686-linux-android$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/i686-linux-android-
PREFIX=$SHELL_PATH/android/$CPU
OPTIMIZE_CFLAGS="-march=i686 -mtune=intel -mssse3 -mfpmath=sse -m32 "
build_android

#x86_64
# ARCH=x86_64
# CPU=x86-64
# CC=$TOOLCHAIN/bin/x86_64-linux-android$API-clang
# CXX=$TOOLCHAIN/bin/x86_64-linux-android$API-clang++
# SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
# CROSS_PREFIX=$TOOLCHAIN/bin/x86_64-linux-android-
# PREFIX=$SHELL_PATH/android/$CPU
# OPTIMIZE_CFLAGS="-march=$CPU -msse4.2 -mpopcnt -m64 -mtune=intel"
# build_android

#armv7-a
ARCH=arm
CPU=armv7-a
CC=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang
CXX=$TOOLCHAIN/bin/armv7a-linux-androideabi$API-clang++
SYSROOT=$NDK/toolchains/llvm/prebuilt/linux-x86_64/sysroot
CROSS_PREFIX=$TOOLCHAIN/bin/arm-linux-androideabi-
PREFIX=$SHELL_PATH/android/$CPU
OPTIMIZE_CFLAGS="-mfloat-abi=softfp -mfpu=vfp -marm -march=$CPU "
build_android